package Servlets;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.JSONException;
import org.json.JSONObject;

import DAL.Entites.Game;
import DAL.JDBC.DBConn;
import DAL.JDBC.GameJDBC;

public class UpdateGame extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    public UpdateGame() {
        super();
    }
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		JSONObject result = new JSONObject();
		GameJDBC gameJDBC = new GameJDBC(DBConn.getConnection());
		
		int id = Integer.parseInt(request.getParameter("id"));
		String name = request.getParameter("name");
		int consoleId = Integer.parseInt(request.getParameter("consoleId"));
		int genreId = Integer.parseInt(request.getParameter("genreId"));
		String description = request.getParameter("description");
		String trailer = request.getParameter("trailer");
		int quantity = Integer.parseInt(request.getParameter("quantity"));
		int price = Integer.parseInt(request.getParameter("price"));
		
		Game gameToUpdate = new Game(id, name, description, trailer, consoleId, genreId, quantity, price, null, "", "");
		
		try {
			if(gameJDBC.updateGame(gameToUpdate) == true)
			{
				result.put("result", true);
			}
			
			else
			{
				result.put("result", false);
				result.put("message", "error in update");
			}
		}
		catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		PrintWriter pw = response.getWriter();
		pw.write(result.toString());
		
	}

}
